package com.library.servlet;

import com.library.entity.Student;
import com.library.service.admin.AdminService;
import com.library.service.student.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/common")
public class CommonServlet extends BaseServlet {

    private final AdminService adminService = new AdminService();
    private final StudentService studentService = new StudentService();

    // 注册
    protected void studentReg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("收到注册请求：" + req.getParameter("sno") + "," + req.getParameter("username"));
        String sno = req.getParameter("sno");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        String collage = req.getParameter("collage");
        String major = req.getParameter("major");
        String birthStr = req.getParameter("birth");
        String originPlace = req.getParameter("originPlace");
        java.sql.Date birth = null;
        if (birthStr != null && !birthStr.isEmpty()) {
            birth = java.sql.Date.valueOf(birthStr);
        }
        Student student = new Student();
        student.setSno(sno);
        student.setUsername(username);
        student.setPassword(password);
        student.setType(type);
        student.setCollage(collage);
        student.setMajor(major);
        student.setBirth(birth);
        student.setOriginPlace(originPlace);
        String result;
        try {
            result = studentService.register(student);
        } catch (Exception e) {
            result = e.getMessage();
        }
        if ("注册成功".equals(result)) {
            req.setAttribute("msg", "注册成功，请登录！");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else {
            req.setAttribute("msg", result);
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        switch (type) {
            case "admin" -> adminLogin(req, resp);
            case "student" -> studentLogin(req, resp);
            default -> throw new RuntimeException("登录身份有误");
        }

    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

    private void adminLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean login = adminService.login(username, password, req);
        if (login) {
            // 成功登录
            System.out.println("管理员登录成功");
            req.getRequestDispatcher("admin?method=adminIndex").forward(req, resp);
            return;
        }

        // 登陆失败
        req.getRequestDispatcher("/index.jsp").forward(req, resp);

    }

    private void studentLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sno = req.getParameter("sno");
        String password = req.getParameter("password");
        boolean login = studentService.login(sno, password, req);
        if (login) {
            // 登录成功
            System.out.println("学生登录成功");
            req.getRequestDispatcher("/WEB-INF/student/example.jsp").forward(req, resp);
            return;
        }

        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

}
